<?php
defined('IN_XLP') or exit('Access Denied!');

class wmOrderDishModel extends Model {

    function __construct() {
        parent::__construct();
        $this->dbTable = 'wm_order_dish';
        $this->page = 20;
    }

    /**
     * 获取订单菜品信息
     */
    public function getOrderDish($order_id, $field='*'){
    	$where  = '`order_id` = ' . $order_id;
    	$result = $this->field($field)->where($where)->findAll();
    	return $result;
    }


    /**
     * 添加订单菜品
     * @param Int $order_id  订单id
     * @param Array $dish_arr 数组(key为菜品id，value为数量)
     * @return Int 新增记录数量
     */
    public function add($order_id, $dish_arr){

        $dish_id_arr  = array();
        $dish_num_arr = array();
        $dish_ids     = '';

        foreach ($dish_arr as $k => $v) {
            $dish_id_arr[]  = $k;
            $dish_num_arr[] = $v;
            $dish_ids      .= $k.',';
        }
        $dish_ids = rtrim($dish_ids, ',');

        $dish_info = M('wm_menu')
                    ->field('`id`,`dish_name`,`dish_price`,`dish_out_img`')
                    ->where("`id` in ($dish_ids)")
                    ->findAll();

        // z($dish_info);
        $insert_arr = array();
        foreach ($dish_info as $k => $v) {
            $insert_arr[] = array(
                'order_id'   => $order_id,
                'dish_id'    => $v['id'],
                'dish_num'   => $dish_num_arr[$k],
                'dish_price' => $v['dish_price'],
                'dish_name'  => $v['dish_name'],
                'dish_img'   => $v['dish_out_img'],
            );
        }

        $result = M('wm_order_dish')->insertAll($insert_arr);
        return $result;
    }

}